<template>
  <div class="shop-r-form">
    <h2 class="pancel-title panel-2 flex v-align-c">
      <span></span>
      <i>图片组</i>
    </h2>
    <h3 class="pancel-s-title">图片设置</h3>
    <el-form class="pad-t-15 pad-l-25 pad-r-25 form-diy">
      <div class="border-box near-box">
        <h4>样式风格</h4>
        <div class="mar-t--5 mar-b--10">
          <el-form-item>
            <div class="font-14">
              <el-row>
                <el-col :span="12">
                  <el-radio v-model="details.propMap.picStyle" label="10" @change="typeChange">单列大图</el-radio>
                </el-col>
                <el-col :span="12">
                  <el-radio v-model="details.propMap.picStyle" label="11" @change="typeChange">单列双图</el-radio>
                </el-col>
              </el-row>
            </div>
          </el-form-item>
        </div>
      </div>
      <div class="border-box">
        <h4>边距</h4>
        <div class="mar-t--5 mar-b--10">
          <el-form-item>
            <div class="v-align-c font-12">
              <span>上下</span>
              <el-slider v-model="details.propMap.verticalMargin" class="flex-item mar-l-15" :max="50"></el-slider>
              <el-input class="mar-l-20 color-sel-input" placeholder="请输入内容" :max="50"
                v-model="details.propMap.verticalMargin" @input="marginChange(details, 'verticalMargin',  $event)">
                <template slot="append">px</template>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item>
            <div class="v-align-c font-12">
              <span>左右</span>
              <el-slider v-model="details.propMap.horizontalMargin" class="flex-item mar-l-15" :max="50"></el-slider>
              <el-input class="mar-l-20 color-sel-input" placeholder="请输入内容" :max="50"
                v-model="details.propMap.horizontalMargin" @input="marginChange(details, 'horizontalMargin', $event)">
                <template slot="append">px</template>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item>
            <div class="v-align-c font-12">
              <span>圆角</span>
              <el-slider v-model="details.propMap.radius" class="flex-item mar-l-15" :max="50"></el-slider>
              <el-input class="mar-l-20 color-sel-input" placeholder="请输入内容" :max="50"
                v-model="details.propMap.radius" @input="marginChange(details, 'radius', $event)">
                <template slot="append">px</template>
              </el-input>
            </div>
          </el-form-item>
        </div>
      </div>
      <div class="border-box">
        <h4>图片</h4>
        <div>
          <div class="pancel-tips-bar">建议图片宽度{{ details.propMap.picStyle === '10' ? '750' : '375' }}px，高度跟随上传图片的高度变化</div>
          <div v-for="(item, index) in details.pictureList" :key="'picture-list-' + index">
            <el-form-item class="border-pic-bar pad-6" v-if="index == 0 || (details.pictureList.length > 1 && details.propMap.picStyle == 11)">
              <div class="flex v-align-c">
                <div class="height-60 mar-r-10">
                  <el-upload class="avatar-uploader"
                  action="/api/common/uploadFiles"
                  accept="image/jpeg,image/gif,image/png,image/bmp"
                  list-type="picture-card-ms"
                  :before-upload="beforeUpload" name="files"
                  :data="token" :show-file-list="false" :on-success="(res)=> handleUploadSuccess(res, index)">
                    <!-- imageView2
                    1:为裁剪图片，0为不裁剪
                    q/75：裁剪后图片品质为75，你可以设置成100
                    ?imageMogr2/thumbnail/360x360/format/webp/quality/100 -->
                    <img v-if="item.imgUrl" :src="item.imgUrl + '?imageView2/1/w/60/h/60/q/100'" width="60px" height="60px">
                    <div v-else class="default-p-pic">
                      <div class="default-p-img"></div>
                      <div class="default-p-t">上传图片</div>
                    </div>
                  </el-upload>
                </div>
                <div class="flex-item mar-r-10">
                  <div class="mini-sbar-input cursor-p" :class="item.linkRemark ? '' : 'default-sbar-input'"
                    @click="linkLayer(item)">{{ item.linkRemark ? item.linkRemark : '请选择链接' }}</div>
                </div>
              </div>
            </el-form-item>
          </div>
        </div>
      </div>
    </el-form>
    <!-- 链接设置弹层 -->
    <el-dialog title="链接选择器" :visible.sync="linkSetVisible" :close-on-press-escape="true" width="850px">
      <!-- appType 13 为主播小程序 -->
      <component-link-layer v-if="appType !== 13" :appType="appType" v-on:selectLink="setLinkTemp"></component-link-layer>
      <component-anchor-link-layer v-else v-on:selectLink="setLinkTemp"></component-anchor-link-layer>
      <div slot="footer" class="dialog-footer" v-if="false">
        <el-button @click="linkSetVisible = false">关闭</el-button>
        <el-button type="primary" @click="setLink">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import elDragDialog from '@/directive/el-dragDialog'
  import { getStrByLength } from '@/utils'
  import { getToken } from '@/utils/auth'
  import ComponentLinkLayer from './../ComponentLinkLayer'
  import ComponentAnchorLinkLayer from './../ComponentAnchorLinkLayer'

  export default {
    components: {
      ComponentLinkLayer,
      ComponentAnchorLinkLayer
    },
    directives: {
      // 拖动窗口
      elDragDialog
    },
    props: {
      details1: {
        type: Object,
        default: null
      },
      appType: {
        type: Number,
        default: 10
      }
    },
    data() {
      return {
        heightArr: [],
        details: {
          detailsId: '',
          pageId: '',
          sortNo: 0,
          componentType: 0,
          propMap: {},
          pictureList: []
        },
        fullScreenLoading: null,
        pictureType: '1',
        // 定义链接选择器基础数据开始
        linkSetVisible: false,
        link: {},
        linkTemp: {}
        // 定义链接选择器基础数据结束
      }
    },
    // 初始化页面
    created() {
      this.resetDetails()
      this.details = this.details1
      if (this.details.propMap) {
        this.$set(this.details.propMap, 'picStyle', this.details.propMap.picStyle ? this.details.propMap.picStyle.toString() : '10')
        this.$set(this.details.propMap, 'verticalMargin', this.details.propMap.verticalMargin ? this.details.propMap.verticalMargin * 1 : 0)
        this.$set(this.details.propMap, 'horizontalMargin', this.details.propMap.horizontalMargin ? this.details.propMap.horizontalMargin * 1 : 0)
        this.$set(this.details.propMap, 'radius', this.details.propMap.radius ? this.details.propMap.radius * 1 : 0)
        this.$set(this.details.propMap, 'height', this.details.propMap.height ? this.details.propMap.height * 1 : 0)
      }
      this.token = { accessToken: getToken() }
      const that = this
      this.$bus.$on('resetDetailsPicture', function(details) {
        that.details = details
        that.$set(that.details.propMap, 'picStyle', details.propMap.picStyle ? details.propMap.picStyle.toString() : '10')
        that.$set(that.details.propMap, 'verticalMargin', details.propMap.verticalMargin ? details.propMap.verticalMargin * 1 : 0)
        that.$set(that.details.propMap, 'horizontalMargin', details.propMap.horizontalMargin ? details.propMap.horizontalMargin * 1 : 0)
        that.$set(that.details.propMap, 'radius', details.propMap.radius ? details.propMap.radius * 1 : 0)
        that.$set(that.details.propMap, 'height', details.propMap.height ? details.propMap.height * 1 : 0)
      })
    },
    methods: {
      marginChange(obj, key, e) {
        // 滑块输入框触发事件
        if (this.$utils.isNumber(e)) {
          if (e * 1 > 50) {
            obj.propMap[key] = 50
          } else {
            obj.propMap[key] = e * 1
          }
        } else {
          obj.propMap[key] = 0
        }
      },
      handleRemove(file, fileList) {
        console.log(file, fileList)
      },
      handlePictureCardPreview(file) {
        console.log(file)
      },
      beforeUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isGIF = file.type === 'image/gif'
        const isPNG = file.type === 'image/png'
        const isBMP = file.type === 'image/bmp'

        if (!isJPG && !isGIF && !isPNG && !isBMP) {
          this.$message.error('上传图片必须是JPG/GIF/PNG/BMP 格式!')
        }
        this.fullScreenLoading = this.$loading({
          lock: true,
          text: '加载中',
          background: 'rgba(255, 255, 255, 0.5)'
        })
        const isSize = new Promise((resolve, reject) => {
          const img = new Image()
          img.onload = function() {
            resolve(img)
          }
          img.src = URL.createObjectURL(file)
        }).then((res) => {
          const width = (369 - this.details.propMap.horizontalMargin) / 2
          this.heightArr.push((res.height * width) / res.width)
          if (this.heightArr.length > 1) {
            this.details.propMap.height = this.maxSort(this.heightArr)
          } else {
            this.details.propMap.height = (res.height * width) / res.width
          }
        })
        return (isJPG || isBMP || isGIF || isPNG || isSize)
      },
      handleUploadSuccess(res, index) {
        this.fullScreenLoading.close()
        if (res.code === 101) {
          this.details.pictureList[index].imgUrl = res.data[0]
          this.$bus.$emit('pictureFormData', this.details)
          console.log(this.details)
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          })
          return false
        }
        console.log(this.details)
      },
      maxSort(arr) {
        arr.sort((a, b) => {
          return b - a
        })
        return arr[0]
      },
      getStrByLength(str, n) {
        return getStrByLength(str, n)
      },
      typeChange() {
        if (this.details.propMap.picStyle * 1 === 11 && this.details.pictureList.length === 1) {
          this.details.pictureList.push({
            pictureId: '',
            imgUrl: '',
            linkType: '',
            linkParams: ''
          })
        }
      },
      linkLayer(item) {
        this.link = item
        this.linkSetVisible = true
      },
      setLinkTemp(item) {
        Object.assign(this.linkTemp, item)
        this.setLink()
      },
      setLink() {
        Object.assign(this.link, this.linkTemp)
        this.linkSetVisible = false
      },
      resetDetails() {
        this.details = {
          detailsId: '',
          pageId: '',
          sortNo: 0,
          componentType: 0,
          backColor: '',
          propMap: {}
        }
      }
    }
  }
</script>
